#!/bin/bash
#JWM Tray Configuration
#Copyright 2008,2009,2010,2014,2015,2016,2017,2018 GPL
#Sigmund Berglund

export TEXTDOMAIN=jwm
export OUTPUT_CHARSET=UTF-8

/usr/lib/gtkdialog/box_splash -close never -fontsize large -icon_width 60 -icon /usr/share/pixmaps/puppy/puppy_config.svg -text " $(gettext 'Loading JWM Tray configuration...')" &
XPID=$!

APPDIR="`dirname $0`"
[ "$APPDIR" = "." ] && APPDIR="`pwd`"
export APPDIR="$APPDIR"
export WORKDIR=$HOME/.jwm/tmp
[ ! -d "$WORKDIR" ] && mkdir -p "$WORKDIR"

#link icons to /usr/share/icons/ - gtkdialog tree doesn't find icons in /usr/share/pixmaps/puppy/
for I in placement_none menu clock desktop_tray_config reboot shutdown desktop screen_lock dialog-info quit; do
	[ ! -s "/usr/share/icons/${I}.svg" ] && ln -s /usr/share/pixmaps/puppy/${I}.svg /usr/share/icons
done

#default values
echo 1 > $WORKDIR/tray_nr
echo 32 > $WORKDIR/tray_SWALLOW_WIDTH
echo '' > $WORKDIR/tray_SWALLOW_HEIGHT
find /usr/share/jwm/tray_templates -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort > $WORKDIR/tray_templates

case $1 in
	-clock)
		PAGE_NR=5
		DEFAULT_CLOCK=true
		DEFAULT_BUTTONS=false
		#detect tray containing clock
		TMP="`grep -lFm 1 '<Clock' $HOME/.jwm/jwmrc-tray* | grep -o [1-4]`"
		echo $TMP > $WORKDIR/tray_nr
		;;
	*)
		PAGE_NR=6 #Buttons
		DEFAULT_CLOCK=false
		DEFAULT_BUTTONS=true
		;;
esac

gettext "<b>In general</b>
This configuration tool handles up to 4 different trays. If active, you see a green arrow showing its placement. The left buttons show the items addable to the tray (list to the right). 

Note!
To see the your changes, you have to press the Apply button.

<b>Manage the list</b>
Remove an item from the list by middle-click on it (or use the right-click menu).
Move items up/down by drag and drop.

<b>Tips for preset builders</b>
Tray1 will be below tray2 (and so on) if set on same layer.

The visible option <i>hybrid</i> is tecnically 2 trays. - One autohidden on the above layer, and one mirrored visible on the below layer. Since JWM only allow one Dock area, you can't include a Dock on a hybrid tray.

Tray 1 is the active configuration and the 'Buttons module' is the active module by startup. Therefor, it will be most logical that an included application-panel (button populated tray) is set to tray 1. We presume that an average user will only do minor changes, and this most likely involves adding/removing application-buttons.

If you want the icons in your tray to follow the theme for menu and desktop icons, you must define icons from <i>/usr/local/lib/X11/pixmaps/</i> instead of the default icon taken from *.desktop files in <i>/usr/share/applications/</i>. Most often, this has to be set manually.

<b>Manual edit</b>
The configuration files for the active tray(s) are
  <i>$HOME/.jwm/jwmrc-tray*</i>
The presets are located at
  <i>/usr/share/jwm/tray_templates/*</i>
" > /tmp/box_help;

#cleanup
rm $WORKDIR/tray_menuicon.svg 2>/dev/null
ln -s /usr/share/pixmaps/puppy/directory.svg $WORKDIR/tray_menuicon.svg

[ ! -f $HOME/.jwm/jwmrc-tray4 ] && echo -n > $HOME/.jwm/jwmrc-tray4
[ ! -f $HOME/.jwm/jwmrc-tray3 ] && echo -n > $HOME/.jwm/jwmrc-tray3
[ ! -f $HOME/.jwm/jwmrc-tray2 ] && echo -n > $HOME/.jwm/jwmrc-tray2
[ ! -f $HOME/.jwm/jwmrc-tray1 ] && echo '
<?xml version="1.0"?>
<JWM>
<Tray autohide="off" insert="right" layer="above" halign="center" valign="bottom" height="28" layout="horizontal" >
    <TrayButton label="Menu" icon="/usr/share/pixmaps/puppy/puppy.svg">root:9</TrayButton>
    <TrayButton popup="Show desktop" icon="/usr/share/pixmaps/puppy/desktop.svg">showdesktop</TrayButton>
    <TrayButton popup="Wizard Wizard" icon="/usr/share/pixmaps/puppy/puppy_config.svg">exec:wizardwizard</TrayButton>
    <TrayButton popup="Terminal emulator" icon="/usr/local/lib/X11/pixmaps/console48.png">exec:defaultterminal</TrayButton>
    <Pager/>
    <TaskList maxwidth="200"/>
    <Dock/>
    <Swallow name="xload" width="32">xload -nolabel -fg "#225F7C" -hl white -bg "#222"</Swallow>
    <Clock format="%I:%M">/usr/local/jwm_config/time</Clock>
</Tray>
</JWM>' > $HOME/.jwm/jwmrc-tray1

#gtk-theme
echo '
style "TextEdit" {
	font_name="normal"
	base[NORMAL]="#222"
	text[NORMAL]="#ccc"
	}
widget "*edit" style "TextEdit"

style "rcTree" {
	fg[NORMAL]			= "#FF0000"
	}
widget "*rcTree" style "rcTree"
' > $WORKDIR/gtkrc
export GTK2_RC_FILES=$WORKDIR/gtkrc:/root/.gtkrc-2.0 #include theme stuff with system themes


  ##########################################################################
  ##                                                                      ##
  ##                           F U N C T I O N S                          ##
  ##                                                                      ##
  ##########################################################################
  
load_preset (){
	PRESET="$1"
	rm $HOME/.jwm/jwmrc-tray*_hybrid
	cp -f /usr/share/jwm/tray_templates/"$PRESET"/jwmrc-tray* $HOME/.jwm/
	[ -s /usr/share/jwm/tray_templates/"$PRESET"/install ] && /usr/share/jwm/tray_templates/"$PRESET"/install
	echo "$PRESET" > $HOME/.jwm/tray_active_preset
	read_settings
	#build hybrid rc files
	for I in 1 2 3 4; do
		if [ "`grep -F '_hybrid</Include>' $HOME/.jwm/jwmrc-tray$I`" ]; then
			grep -vF '_hybrid</Include>' $HOME/.jwm/jwmrc-tray$I | sed -e 's%autohide="\(top\|bottom\|left\|right\)" %autohide="off"%' -e "s%layer=\"above\"%layer=\"below\"%" > $HOME/.jwm/jwmrc-tray${I}_hybrid
		fi
	done
	#---
	apply
}

save_preset (){
	export JWM_tray='
	<window title="'$(gettext 'Save')'" icon-name="gtk-save">
	<vbox>
	  <frame '$(gettext 'Save tray setup')'>
	    <text><label>"'$(gettext 'Note!
This will save the last APPLIED tray setup.')'
"</label></text> 
	    <hbox>
	      <text><label>'$(gettext 'Name')'</label></text> 
	      <comboboxentry space-expand="false" space-fill="false">
	        <variable>PRESET</variable>
	        <default>"'$(<$HOME/.jwm/tray_active_preset)'"</default>
	        <input file>'$WORKDIR'/tray_templates</input>
	        <action>save</action>
	      </comboboxentry>
	    </hbox>
	  </frame>
	  <hbox>
	    <button cancel></button>
	    <button can-default="true" has-default="true" use-stock="true">
	      <label>'$(gettext 'Save')'</label>
	      <input file stock="gtk-save"></input>
	      <action type="exit">save</action>
	    </button>
	  </hbox>
	</vbox>
	</window>'
	I=$IFS; IFS=""
	for STATEMENTS in $(gtkdialog -p JWM_tray --center); do
		eval $STATEMENTS
	done
	IFS=$I
	if [ "$EXIT" = "save" ]; then
		if [ ! "$PRESET" ]; then
			. /usr/lib/gtkdialog/box_ok "$(gettext 'JWM tray')" error "$(gettext 'No name defined')"
			exit
		elif [ -d /usr/share/jwm/tray_templates/"$PRESET"/ ]; then #already exist
			. /usr/lib/gtkdialog/box_yesno "$(gettext 'JWM tray')" "<b>$(gettext 'Template already exist')</b>" "$(gettext 'Do you want to overwrite it?')"
			[ $EXIT != yes ] && exit
		else
			mkdir -p /usr/share/jwm/tray_templates/"$PRESET"/
		fi
		cp -f $HOME/.jwm/jwmrc-tray* /usr/share/jwm/tray_templates/"$PRESET"/
		find /usr/share/jwm/tray_templates -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort > $WORKDIR/tray_templates
		echo "$PRESET" > $HOME/.jwm/tray_active_preset
		echo " $(gettext 'Current tray preset'): $PRESET" > $WORKDIR/tray_statusbar
	fi
}

delete_preset (){
	if [ ! -d /usr/share/jwm/tray_templates/"$1"/ ]; then
		. /usr/lib/gtkdialog/box_ok "$(gettext 'JWM tray')" error "$(gettext 'Nothing to delete')"
	else
		. /usr/lib/gtkdialog/box_yesno "$(gettext 'JWM tray')" "$(gettext 'Do you really want to delete the Tray template')" "<b>$1</b>"
		[ $EXIT != yes ] && exit
		rm -fr /usr/share/jwm/tray_templates/"$1"
		find /usr/share/jwm/tray_templates -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort > $WORKDIR/tray_templates
	fi
}

add (){
	case $1 in
	Tray)
		#include header lines - if any
		grep -iFB 10 -m 1 '<tray ' $WORKDIR/tray_tree | grep -viF '<tray ' | grep -vF '_hybrid</Include>' > $WORKDIR/tray_tmp1
		grep -iFA 100 -m 1 '<tray ' $WORKDIR/tray_tree | grep -viF '<tray ' > $WORKDIR/tray_tmp
		HALIGN="$(<$WORKDIR/tray_HALIGN)"
		VALIGN="$(<$WORKDIR/tray_VALIGN)"
		HYBRID=false
		case $VISIBILITY in
			$(gettext 'Always visible'))
				AUTOHIDE=false
				LAYER=above
				;;
			$(gettext 'Hidden when not in use'))
				AUTOHIDE=true
				LAYER=above
				;;
            $(gettext 'Visible behind running apps'))
				AUTOHIDE=false
				LAYER=below
				;;
			$(gettext 'Hybrid: Visible behind, hidden above'))
				AUTOHIDE=true
				LAYER=above
				HYBRID=true
				if [ "`grep -F 'Dock' $WORKDIR/tray_tree`" ]; then
					. /usr/lib/gtkdialog/box_ok "$(gettext 'JWM tray')" error "$(gettext "<b>Can't set visibility</b>. You can not set visibility to Hybrid as long as the tray contains a dock area.")"
					exit
				fi
				;;
		esac
		if [ $AUTOHIDE = true ]; then
			if [ $VALIGN = top ] && [ $DIRECTION_HOR = true ]; then
				AUTOHIDE=top
			elif [ $VALIGN = top ] && [ $DIRECTION_VER = true ] && [ $HALIGN = center ]; then
				AUTOHIDE=top
			elif [ $VALIGN = bottom ] && [ $DIRECTION_HOR = true ]; then
				AUTOHIDE=bottom
			elif [ $VALIGN = bottom ] && [ $DIRECTION_VER = true ] && [ $HALIGN = center ]; then
				AUTOHIDE=bottom
			elif [ $HALIGN = left ]; then
				AUTOHIDE=left
			elif [ $HALIGN = right ]; then
				AUTOHIDE=right
			else
				AUTOHIDE=off
			fi
		else
			AUTOHIDE=off
		fi
		read HEIGHT < $WORKDIR/tray_HEIGHT
		read WIDTH < $WORKDIR/tray_WIDTH
		[ "$HYBRID" = "true" ] && echo "||<Include>$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)_hybrid</Include>" >> $WORKDIR/tray_tmp1
		echo -e "||<Tray autohide=\"$AUTOHIDE\" insert=\"right\" layer=\"$LAYER\" halign=\"$HALIGN\" valign=\"$VALIGN\" \c" >> $WORKDIR/tray_tmp1
		[ $DIRECTION_HOR = true ] && echo -e "height=\"$HEIGHT\" layout=\"horizontal\" \c"  >> $WORKDIR/tray_tmp1
		[ $DIRECTION_VER = true ] && echo -e "width=\"$WIDTH\" layout=\"vertical\" \c" >> $WORKDIR/tray_tmp1
		echo -e ">" >> $WORKDIR/tray_tmp1
		cat $WORKDIR/tray_tmp >> $WORKDIR/tray_tmp1
		mv $WORKDIR/tray_tmp1 $WORKDIR/tray_tree
		;;
	Menu)
		echo -n "menu|"$(gettext 'Menu')": ${MENU_LABEL}|<TrayButton label=\"${MENU_LABEL}\"" >> $WORKDIR/tray_tree
		if [ -f "$MENU_ICON" ] && [ "$MENU_ICON_CHK" = "true" ]; then echo -n " icon=\"$MENU_ICON\"" >> $WORKDIR/tray_tree; fi
		case $2 in
		$(gettext 'Applications'))
			echo '>root:9</TrayButton>' >> $WORKDIR/tray_tree
			;;
		$(gettext 'Bookmarks'))
			echo '>root:6</TrayButton>' >> $WORKDIR/tray_tree
			;;
		$(gettext 'Places'))
			echo '>root:8</TrayButton>' >> $WORKDIR/tray_tree
			;;
		esac
		;;
	Dock\ area)
		#error checking: jwm supports only one dock area
		if [ "$VISIBILITY" = "$(gettext 'Hybrid: Visible behind, hidden above')" ]; then
			. /usr/lib/gtkdialog/box_ok "$(gettext 'JWM tray')" error "$(gettext "<b>Can't add Dock</b>. It is not possible to include a dock area as long as tray visibility is set to 'Hybrid'")"
			exit
		fi
		TMP="`grep -Fl 'Dock' $HOME/.jwm/jwmrc-tray* | grep -o [1-4]`" #returns nr of trayrc containing <Dock>
		if [ "$TMP" ] && [ "$TMP" != "$(<$WORKDIR/tray_nr)" ]; then
			. /usr/lib/gtkdialog/box_ok "$(gettext 'JWM tray')" error "$(gettext "<b>Can't add Dock</b>. There can only be one dock area, and it is present in another tray in current preset.")"
			exit
		fi
		if [ "`grep -F 'Dock' $WORKDIR/tray_tree`" ]; then
			. /usr/lib/gtkdialog/box_ok "$(gettext 'JWM tray')" error "$(gettext "<b>Can't add Dock</b>. There can only be one dock area. Already in current tray.")"
			exit
		fi
		#---
		echo 'placement_none|'$(gettext 'Dock')'|<Dock/>' >> $WORKDIR/tray_tree
		;;
	Tasklist)
		case $TASKLIST_STYLE in
			"$(gettext 'Iconify')")			TMP=25;;
			"$(gettext 'Fixed width')")		TMP=200;;
			*)								TMP=0;;
		esac
		if [ "`grep -F '<TaskList' $WORKDIR/tray_tree`" ]; then
			TMP1="`grep '<TaskList' $WORKDIR/tray_tree`"
			sed -i "s%$TMP1%placement_none|$(gettext 'Tasklist')|<TaskList maxwidth=\"$TMP\"/>%" $WORKDIR/tray_tree
		else
			echo 'placement_none|'$(gettext 'Tasklist')'|<TaskList maxwidth="'$TMP'"/>' >> $WORKDIR/tray_tree
		fi
		;;
	Spacer)
		echo "|  $(gettext 'Spacer')|<Spacer width=\"$SPACER\" height=\"$SPACER\"/> <!-- $(date +%s) -->" >> $WORKDIR/tray_tree
		;;
	Clock)
		[ ! "$CLOCK" ] && CLOCK="24:mm"
		if [ "$CLOCK_ACTION" = "$(gettext 'Clock tools')" ]; then
			CLOCK_ACTION="exec:/usr/local/jwm_config/time"
		elif [ ! "$CLOCK_ACTION" ] || [ "$CLOCK_ACTION" = "$(gettext 'Clock menu')" ]; then
			CLOCK_ACTION="root:7"
		elif [ "$CLOCK_ACTION" = "$(gettext 'Show desktop')" ]; then
			CLOCK_ACTION="showdesktop"
		else
			CLOCK_ACTION="exec:$CLOCK_ACTION"
		fi
		TMP=''
		case "$CLOCK" in
		$(gettext 'full date and time'))
			TMP=$TMP'%c'
			;;
		*)
			case "$CLOCK" in *$(gettext 'day')*) TMP=$TMP'%a ';; esac
			case "$CLOCK" in
				*$(gettext 'date/month')*)
					TMP=$TMP'%d/%m '
					;;
				*)
					case "$CLOCK" in *$(gettext 'date')*) TMP=$TMP'%d. ';; esac
					case "$CLOCK" in *$(gettext 'month')*) TMP=$TMP'%b ';; esac
					;;
			esac
			case "$CLOCK" in *12*) TMP=$TMP'%I';; esac
			case "$CLOCK" in *24*) TMP=$TMP'%H';; esac
			case "$CLOCK" in *mm*) TMP=$TMP':%M';; esac
			case "$CLOCK" in *ss*) TMP=$TMP':%S';; esac
			case "$CLOCK" in *AM*) TMP=$TMP':%p';; esac
			;;
		esac
		if [ "`grep -F '<Clock' $WORKDIR/tray_tree`" ]; then
			TMP1="`grep '<Clock' $WORKDIR/tray_tree`"
			sed -i "s#${TMP1}#clock|$(gettext 'Clock')|<Clock format=\"$TMP\">${CLOCK_ACTION}</Clock>#" $WORKDIR/tray_tree
		else
			echo "clock|$(gettext 'Clock')|<Clock format=\"${TMP}\">${CLOCK_ACTION}</Clock>" >> $WORKDIR/tray_tree
		fi
		;;
	Pager)
		if [ "`grep -F '<Desktops ' $HOME/.jwm/jwmrc-personal`" ]; then
			TMP="`grep '<Desktops ' $HOME/.jwm/jwmrc-personal`"
			sed -i "s%$TMP%<Desktops width=\"$PAGER_COLUMNS\" height=\"$PAGER_ROWS\"/>%" $HOME/.jwm/jwmrc-personal
		else
			TMP="`grep -i '<JWM>' $HOME/.jwm/jwmrc-personal`"
			sed -i "s%$TMP%<JWM>\n\n<!-- Number of virtual desktops -->\n<Desktops width=\"$PAGER_COLUMNS\" height=\"$PAGER_ROWS\"/>%" $HOME/.jwm/jwmrc-personal
		fi
		[ ! "`grep -F 'Pager' $WORKDIR/tray_rc`" ] && echo 'placement_none|'$(gettext 'Pager')'|<Pager/>' >> $WORKDIR/tray_rc
		;;
	Function\ button)
		case $FUNCTION in
		$(gettext 'Show desktop'))
			echo 'desktop|'$(gettext 'Button:')" $FUNCTION"'|<TrayButton popup="'$(gettext 'Show desktop')'" icon="/usr/share/pixmaps/puppy/desktop.svg">showdesktop</TrayButton>' >> $WORKDIR/tray_tree
			;;
		$(gettext 'Lock screen'))
			echo 'screen_lock|'$(gettext 'Button:')" $FUNCTION"'|<TrayButton popup="'$(gettext 'Lock screen')'" icon="/usr/share/pixmaps/puppy/screen_lock.svg">exec:/usr/local/apps/Xlock/AppRun</TrayButton>' >> $WORKDIR/tray_tree
			;;
		$(gettext 'Puppy Help'))
			echo 'dialog-info|'$(gettext 'Button:')" $FUNCTION"'|<TrayButton popup="'$(gettext 'Puppy Help')'" icon="/usr/share/pixmaps/puppy/dialog-info.svg">/usr/sbin/indexgen.sh open</TrayButton>' >> $WORKDIR/tray_tree
			;;
		$(gettext 'Leave'))
			echo 'quit|'$(gettext 'Button:')" $FUNCTION"'|<TrayButton popup="'$(gettext 'Leave Puppy')'" icon="/usr/share/pixmaps/puppy/quit.svg">exec:/usr/sbin/logout_gui</TrayButton>' >> $WORKDIR/tray_tree
			;;
		$(gettext 'Shutdown NOW'))
			echo 'shutdown|'$(gettext 'Button:')" $FUNCTION"'|<TrayButton popup="'$(gettext 'Power OFF computer')'" icon="/usr/share/pixmaps/puppy/shutdown.svg">exec:wmpoweroff</TrayButton>' >> $WORKDIR/tray_tree
			;;
		$(gettext 'Reboot NOW'))
			echo 'reboot|'$(gettext 'Button:')" $FUNCTION"'|<TrayButton popup="'$(gettext 'Reboot computer')'" icon="/usr/share/pixmaps/puppy/reboot.svg">exec:wmreboot</TrayButton>' >> $WORKDIR/tray_tree
			;;
		$(gettext 'Edit tray'))
			echo 'desktop_tray_config|'$(gettext 'Button:')" $FUNCTION"'|<TrayButton popup="'$(gettext 'Edit tray')'" icon="/usr/share/pixmaps/puppy/desktop_tray_config.svg">exec:/usr/local/jwm_config/tray</TrayButton>' >> $WORKDIR/tray_tree
			;;
		Theme*)
			THEME="`cut -d: -f2- <<< "$FUNCTION"`"
			ICON="`grep -F 'ICON=' "/usr/share/ptheme/globals/${THEME}" | cut -d'=' -f2 | tr -d '"'`"
			echo "$(basename "$ICON" | sed -e 's/\.[^\.]*$//')|$(gettext 'Button:') $FUNCTION"'|<TrayButton popup="'$(gettext 'Puppy global theme')'" icon="'$ICON'">exec:ptheme -p "'$THEME'"</TrayButton>' >> $WORKDIR/tray_tree
			[ ! -s "/usr/share/icons/$(basename "$ICON")" ] && ln -s "$ICON" /usr/share/icons
			;;
		esac
		;;
	Program\ button)
		EXEC="$(grep -iFm 1 'Exec=' "/usr/share/applications/${PROGRAM}.desktop" | cut -d'=' -f2)"
		POPUP="$(grep -iFm 1 'Name=' "/usr/share/applications/${PROGRAM}.desktop" | cut -d'=' -f2)" #find description
		[ ! "$POPUP" ] && POPUP="$EXEC"
		NAME="$(grep -iFm 1 'GenericName=' "/usr/share/applications/${PROGRAM}.desktop" | cut -d'=' -f2)"
		[ ! "$NAME" ] && NAME="$POPUP"
		ICON_PATH="$(grep -iFm 1 'Icon=' "/usr/share/applications/${PROGRAM}.desktop" | cut -d= -f2)"
		if [ -f "$ICON_PATH" ]; then
			ICON="$(basename "$ICON_PATH" | sed -e 's/\.[^\.]*$//')"
			[ ! -s "/usr/share/icons/$(basename "$ICON_PATH")" ] && ln -s "$ICON_PATH" /usr/share/icons
		else
			ICON="$(echo "$ICON_PATH" | sed -e 's/\.[^\.]*$//')" #no extension
		fi
		#add item to list
		echo "${ICON}|$(gettext 'Button'):${NAME}|<TrayButton popup=\"${POPUP}\" icon=\"${ICON_PATH}\">exec:${EXEC}</TrayButton>" >> $WORKDIR/tray_tree
		;;
	Swallowed)
		SWALLOW_NAME="`cut -d' ' -f1 <<< "$SWALLOW"`"
		[ "$SWALLOW_HEIGHT" ] && TMP="height=\"$SWALLOW_HEIGHT\""
		[ "$SWALLOW_WIDTH" ] && TMP1="width=\"$SWALLOW_WIDTH\""
		echo -e 'placement_none|'$(gettext 'Swallow')'|<Swallow name="'${SWALLOW_NAME}'" '${TMP}' '${TMP1}'>'${SWALLOW}'</Swallow>' >> $WORKDIR/tray_tree
		;;
	esac
}

apply (){
	LANG=C
	cut -d"|" -f3 $WORKDIR/tray_tree > $WORKDIR/tray_rc 
	echo -e "<?xml version="'"1.0"'"?>\n<JWM>\n" > "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"
	if [ "`grep -Fiv '<tray ' $WORKDIR/tray_rc | grep -v "^$"`" ]; then #with an empty <tray> tag, video fullscreen fails.
		cat $WORKDIR/tray_rc >> "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"
		echo '</Tray>' >> "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"
	fi
	echo '</JWM>' >> "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"
	#hybrid: mirror existing tray autohidden on top
	if [ "`grep -F '_hybrid</Include>' $HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)`" ]; then
		grep -vF '_hybrid</Include>' $HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr) | sed -e 's%autohide="\(top\|bottom\|left\|right\)" %autohide="off"%' -e "s%layer=\"above\"%layer=\"below\"%" > $HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)_hybrid
	else
		[ -s $HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)_hybrid ] && rm $HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)_hybrid
	fi
	#---
	set_icons
	$APPDIR/jwm_restart
}

read_settings (){
	#visibility
	[ "`LANG=C grep -F '_hybrid</Include>' $HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)`" ] && HYBRID=true
	LAYER="`LANG=C grep -o " layer=.* " "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | cut -d'"' -f2`"
	AUTOHIDE="`LANG=C grep -o " autohide=.* " "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | cut -d'"' -f2`"
	if [ "$HYBRID" = "true" ]; then
		gettext 'Hybrid: Visible behind, hidden above' > $WORKDIR/tray_VISIBILITY
	elif [ "$LAYER" = "below" ]; then
		gettext 'Visible behind running apps' > $WORKDIR/tray_VISIBILITY
	elif [ "$AUTOHIDE" != "off" ] && [ "$AUTOHIDE" ]; then
		gettext 'Hidden when not in use' > $WORKDIR/tray_VISIBILITY
	else
		gettext 'Always visible' > $WORKDIR/tray_VISIBILITY
	fi
	#Tasklist
	TMP="`LANG=C grep -F '<TaskList' "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | grep -F maxwidth | cut -d'"' -f2`"
	[ ! "$TMP" ] && TMP=0
	case $TMP in
		25)		echo "$(gettext 'Iconify')" > $WORKDIR/tray_TASKLIST_STYLE;;
		200)	echo "$(gettext 'Fixed width')" > $WORKDIR/tray_TASKLIST_STYLE;;
		*)		echo "$(gettext 'Variable width')" > $WORKDIR/tray_TASKLIST_STYLE;;
	esac
	
	#statusbar
	echo " $(gettext 'Current tray preset'): $(<$HOME/.jwm/tray_active_preset)" > $WORKDIR/tray_statusbar
	
	#build <tree>
	echo -n > $WORKDIR/tray_tree
	while read I; do
		case $I in
			*TrayButton*root:*)
				DESCR="$(gettext 'Menu'): $(echo "$I" | awk -F label= '{print $2}' | cut -d'"' -f2)"
				ICON="menu"
				;;
			*TrayButton*)
				DESCR="$(gettext 'Button'): $(LANG=C basename "$(echo "$I" | cut -d'>' -f2 | cut -d'<' -f1 | sed 's%exec:%%')")"
				ICON_PATH="`echo "$I" | awk -F icon= '{print $2}' | cut -d'"' -f2`"
				[ ! -f "$ICON_PATH" ] && ICON_PATH="/usr/share/pixmaps/$(basename "$ICON_PATH")"
				[ ! -f "$ICON_PATH" ] && ICON_PATH="/usr/share/pixmaps/puppy/$(basename "$ICON_PATH")"
				ICON="$(LANG=C basename "$ICON_PATH" | sed -e 's/\.[^\.]*$//')"
				[[ -f "$ICON_PATH" && ! -s "/usr/share/icons/$(basename "$ICON_PATH")" ]] && ln -s "$ICON_PATH" /usr/share/icons
				;;
			*Tray*)
				DESCR=""
				ICON=""
				;;
			*TaskList*)
				DESCR="$(gettext 'Tasklist')"
				ICON="placement_none"
				;;
			*Pager*)
				DESCR="$(gettext 'Pager')"
				ICON="placement_none"
				;;
			*Dock*)
				DESCR="$(gettext 'Dock')"
				ICON="placement_none"
				;;
			*Swallow*)
				DESCR="$(gettext 'Swallow')"
				ICON="placement_none"
				;;
			*Clock*)
				DESCR="$(gettext 'Clock')"
				ICON="clock"
				;;
			*Spacer*)
				ICON=""
				DESCR="  $(gettext 'Spacer')"
				;;
			*)
				ICON=""; DESCR="";;
		esac
		echo "$ICON|$DESCR|$I" >> $WORKDIR/tray_tree
	done <<< "`LANG=C grep -vEi "jwm>|/tray>|<?xml" "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | grep "[a-z]"`"
	#---
	LANG=C
	#placement
	[  ! "`grep -Fi 'valign=' $HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)`" ] && sed -i 's%halign%valign="bottom" halign%' "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" #valign="bottom" is the default value
	TMP2="`grep -Fi '<Tray' "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"`"
	TMP="`grep -om1 " halign=.* " <<< "$TMP2" | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_HALIGN || echo center > $WORKDIR/tray_HALIGN
	TMP="`grep -om1 " valign=.* " <<< "$TMP2" | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_VALIGN || echo bottom > $WORKDIR/tray_VALIGN
	TMP="`grep -om1 " height=.* " <<< "$TMP2" | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_HEIGHT || echo 28 > $WORKDIR/tray_HEIGHT
	TMP="`grep -om1 " width=.* " <<< "$TMP2" | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_WIDTH || echo 28 > $WORKDIR/tray_WIDTH
	#direction
	if [ "`grep -F 'layout="vertical"' "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"`" ]; then
		echo true > $WORKDIR/tray_DIRECTION_VER
		echo false > $WORKDIR/tray_DIRECTION_HOR
	else
		echo true > $WORKDIR/tray_DIRECTION_HOR
		echo false > $WORKDIR/tray_DIRECTION_VER
	fi
	#pager
	TMP="`grep -F '<Desktops ' $HOME/.jwm/jwmrc-personal | cut -d'"' -f4`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_PAGER_ROWS || echo 1 > $WORKDIR/tray_PAGER_ROWS
	TMP="`grep -F '<Desktops ' $HOME/.jwm/jwmrc-personal | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_PAGER_COLUMNS || echo 3 > $WORKDIR/tray_PAGER_COLUMNS
	#
	set_icons
	set_placement_icons
}

set_placement_icons (){
	#show active placement button
	#copy placement icons for buttons
	for I in topleft top topright left none right bottomleft bottom bottomright; do
		cp -f /usr/share/pixmaps/puppy/placement_$I.svg $WORKDIR/placement_$I.svg
	done
	#---
	[  ! "`grep -Fi 'valign=' $WORKDIR/tray_tree`" ] && sed -i 's%halign%valign="bottom" halign%' $WORKDIR/tray_tree #valign="bottom" is the default value
	TMP="$(<$WORKDIR/tray_tree)"
	case "$TMP" in
	*valign=\"bottom\"*)
		case "$TMP" in
		*halign=\"left\"*)	TMP1=bottomleft;;
		*halign=\"right\"*)	TMP1=bottomright;;
		*)					TMP1=bottom;;
		esac;;
	*valign=\"top\"*)
		case "$TMP" in
		*halign=\"left\"*)	TMP1=topleft;;
		*halign=\"right\"*)	TMP1=topright;;
		*)					TMP1=top;;
		esac;;
	*)
		case "$TMP" in
		*halign=\"left\"*)	TMP1=left;;
		*halign=\"right\"*)	TMP1=right;;
		*)					TMP1=none;;
		esac;;
	esac
	[ "$TMP1" != "none" ] && sed -i 's%<path style="fill:none%<path style="fill:none;stroke:#00B908;stroke-width:25;stroke-opacity:0.5" d="M 2,2 98,2 98,98 2,98 z"/><path style="fill:none%' $WORKDIR/placement_$TMP1.svg
}

set_icons (){
	LANG=C
	#set placement icons for tray-setup-buttons
	for I in 1 2 3 4; do
		[  ! "`grep -Fi 'valign=' $HOME/.jwm/jwmrc-tray$I`" ] && sed -i 's%halign%valign="bottom" halign%' $HOME/.jwm/jwmrc-tray$I #valign="bottom" is the default value
		TMP="$(<$HOME/.jwm/jwmrc-tray$I)"
		case "$TMP" in
		*valign=\"bottom\"*)
			case "$TMP" in
			*halign=\"left\"*)	cp /usr/share/pixmaps/puppy/placement_bottomleft.svg $WORKDIR/tray_$I.svg;;
			*halign=\"right\"*)	cp /usr/share/pixmaps/puppy/placement_bottomright.svg $WORKDIR/tray_$I.svg;;
			*)					cp /usr/share/pixmaps/puppy/placement_bottom.svg $WORKDIR/tray_$I.svg;;
			esac;;
		*valign=\"top\"*)
			case "$TMP" in
			*halign=\"left\"*)	cp /usr/share/pixmaps/puppy/placement_topleft.svg $WORKDIR/tray_$I.svg;;
			*halign=\"right\"*)	cp /usr/share/pixmaps/puppy/placement_topright.svg $WORKDIR/tray_$I.svg;;
			*)					cp /usr/share/pixmaps/puppy/placement_top.svg $WORKDIR/tray_$I.svg;;
			esac;;
		*)
			case "$TMP" in
			*halign=\"left\"*)	cp /usr/share/pixmaps/puppy/placement_left.svg $WORKDIR/tray_$I.svg;;
			*halign=\"right\"*)	cp /usr/share/pixmaps/puppy/placement_right.svg $WORKDIR/tray_$I.svg;;
			*)					cp /usr/share/pixmaps/puppy/placement_none.svg $WORKDIR/tray_$I.svg;;
			esac;;
		esac
	done
	#show active tray
	sed -i 's%<path style="fill:none%<path style="fill:none;stroke:#00B908;stroke-width:25;stroke-opacity:0.5" d="M 2,2 98,2 98,98 2,98 z"/><path style="fill:none%' $WORKDIR/tray_$(<$WORKDIR/tray_nr).svg
}

release_event (){
	#right-click menu
	if [ $BUTTON = 3 ]; then
		if [ "$TRAY" ]; then
			treemenu "$TRAY"
		fi
		exit
	fi
	#---
	PRESS_EVENT="$(<"$WORKDIR/tray_press_event")"
	RELEASE_EVENT="`grep -F "$TRAY" $WORKDIR/tray_tree`"
	HEADER="`grep -Fm1 -B10 '<Tray ' $WORKDIR/tray_tree`" #no items can't move above header line(s)
	if [ "$TRAY" ] && [ "`grep "$PRESS_EVENT" <<< "$HEADER"`" ] && [ "$RELEASE_EVENT" != "$PRESS_EVENT" ]; then
		. /usr/lib/gtkdialog/box_ok "$(gettext 'JWM tray')" error "$(gettext "Can't (re)move header line(s)")"
		exit
	fi 
	#button mid-click
	if [ $BUTTON = 2 ]; then
		if [ "$TRAY" ]; then
			grep -vF "$TRAY" $WORKDIR/tray_tree > $WORKDIR/tray_tmp
			mv $WORKDIR/tray_tmp $WORKDIR/tray_tree
		fi
		exit
	fi
	#Button left-click
	edit
}

treemenu (){
	TRAY="$1"
	rm $WORKDIR/tray_menu_OUTPUT
	export gtkdialog_menu='
	<window decorated="false" skip_taskbar_hint="true" resizable="false" border-width="0" space-expand="true" space-fill="true">
	  <eventbox name="rightclick_menu_frame" above-child="false">
	    <vbox border-width="1">
	      <eventbox>
	        <vbox spacing="0" border-width="7">

	          <eventbox above-child="true" visible-window="true">
	            <hbox spacing="7" border-width="3">
	              <pixmap space-expand="false" space-fill="false"><height>18</height><input file>/usr/share/pixmaps/puppy/remove.svg</input></pixmap>
	              <text xalign="0" space-expand="true" space-fill="true"><label>"'$(gettext 'Remove (mouse mid-click)')'"</label></text>
	            </hbox>
	            <action signal="enter-notify-event">hide:MENU_1</action>
	            <action signal="enter-notify-event">show:MENU_1B</action>
	          <variable>MENU_1</variable>
	          </eventbox>
	          <eventbox name="rightclick_menu" above-child="true" visible-window="true" visible="false">
	            <hbox spacing="7" border-width="3">
	              <pixmap space-expand="false" space-fill="false"><height>18</height><input file>/usr/share/pixmaps/puppy/remove.svg</input></pixmap>
	              <text xalign="0" space-expand="true" space-fill="true"><label>"'$(gettext 'Remove (mouse mid-click)')'"</label></text>
	            </hbox>
	            <action signal="leave-notify-event">show:MENU_1</action>
	            <action signal="leave-notify-event">hide:MENU_1B</action>
	            <action signal="button-release-event">echo remove > '$WORKDIR'/tray_menu_OUTPUT</action>
	            <action signal="button-release-event">EXIT:exit</action>
	            <variable>MENU_1B</variable>
	          </eventbox>

	          <hseparator height-request="5"></hseparator>

	          <eventbox above-child="true" visible-window="true">
	            <hbox spacing="7" border-width="3">
	              <pixmap space-expand="false" space-fill="false"><height>18</height><input file>/usr/share/pixmaps/puppy/edit.svg</input></pixmap>
	              <text xalign="0" space-expand="true" space-fill="true"><label>"'$(gettext 'Open copy in text editor')'"</label></text>
	            </hbox>
	            <action signal="enter-notify-event">hide:MENU_2</action>
	            <action signal="enter-notify-event">show:MENU_2B</action>
	            <variable>MENU_2</variable>
	          </eventbox>
	          <eventbox name="rightclick_menu" above-child="true" visible-window="true" visible="false">
	            <hbox spacing="7" border-width="3">
	              <pixmap space-expand="false" space-fill="false"><height>18</height><input file>/usr/share/pixmaps/puppy/edit.svg</input></pixmap>
	              <text xalign="0" space-expand="true" space-fill="true"><label>"'$(gettext 'Open copy in text editor')'"</label></text>
	            </hbox>
	            <action signal="leave-notify-event">show:MENU_2</action>
	            <action signal="leave-notify-event">hide:MENU_2B</action>
	            <action signal="button-release-event">echo edit > '$WORKDIR'/tray_menu_OUTPUT</action>
	            <action signal="button-release-event">EXIT:exit</action>
	            <variable>MENU_2B</variable>
	          </eventbox>

	        </vbox>
	      </eventbox>
	    </vbox>
	  </eventbox>
	  <action signal="focus-out-event">EXIT:exit</action>
	</window>'

	#set extended gtk-theme
	echo 'style "Rightclick_menu" { bg[NORMAL] = "#888" }
	style "Rightclick_menu_frame" { bg[NORMAL] = "#000" }
	widget "*rightclick_menu" style "Rightclick_menu"
	widget "*rightclick_menu_frame" style "Rightclick_menu_frame"
	' > $WORKDIR/gtkrc_menu
	export GTK2_RC_FILES=$WORKDIR/gtkrc_menu:/root/.gtkrc-2.0

	gtkdialog -p gtkdialog_menu -G +"$(($PTR_X_ROOT-5))"+"$(($PTR_Y_ROOT-5))" > /dev/null
	OUTPUT=($(<"$WORKDIR/tray_menu_OUTPUT"))
	case "$OUTPUT" in
	remove)
		if [ -s "$WORKDIR/tray_menu_OUTPUT" ] ; then
			if [ "$TRAY" ]; then
				grep -vF "$TRAY" $WORKDIR/tray_tree > $WORKDIR/tray_tmp
				mv $WORKDIR/tray_tmp $WORKDIR/tray_tree
			fi
			exit
		fi
		;;
	edit)
		open_copy
		;;
	esac
}

open_copy (){
   echo -e '<?xml version="1.0"?>\n<JWM>' > $WORKDIR/jwmrc_tray-TEMPORARY
   cut -d'|' -f3 < $WORKDIR/tray_tree >> $WORKDIR/jwmrc_tray-TEMPORARY
   echo -e '</Tray>\n</JWM>' >> $WORKDIR/jwmrc_tray-TEMPORARY
   defaulttexteditor $WORKDIR/jwmrc_tray-TEMPORARY &
   sleep 3
   rm $WORKDIR/jwmrc_tray-TEMPORARY
}

edit(){
	[ ! "$TRAY" ] && exit
	echo "$TRAY" > $WORKDIR/TRAY
	#geometry
	XWININFO=`xwininfo -stats -name $(gettext 'Tray-configuration')`
	WIDTH=$(((`echo "$XWININFO" | grep 'Width:' | awk '{print $2}'`)-520))
	export X=$(((`echo "$XWININFO" | grep 'Absolute upper-left X' | awk '{print $4}'`)+515))
	export Y=$(((`getcurpos | awk '{print $2}'`)-20))
	#---
	export Tray_edit='
	<window height-request="35" width-request="'$WIDTH'" skip-taskbar-hint="true" decorated="false">
	  <hbox space-fill="true" space-expand="true" tooltip-text="'$(gettext 'Apply changes by pressing ENTER')'">
	   <entry width-request="1" height-request="1" space-fill="false" space-expand="false"></entry>
	   <timer visible="false" milliseconds="true" interval="300">
	    <action condition="command_is_true(MOUSEX=`getcurpos | awk '"'"'{print $1}'"'"'`; MOUSEY=`getcurpos | awk '"'"'{print $2}'"'"'`; [[ $MOUSEX -lt $X || $MOUSEX -gt '$(($X+$WIDTH))' || $MOUSEY -lt '$(($Y-5))' || $MOUSEY -gt '$(($Y+35))' ]] && echo true )">EXIT:exit</action>
	   </timer>
	   <entry name="edit" has_frame="false" space-fill="true" space-expand="true">
	     <variable>EDIT</variable>
	     <input file>'$WORKDIR'/TRAY</input>
	     <output file>'$WORKDIR'/TRAY_out</output>
	     <action signal="activate">save:EDIT</action>
	     <action signal="activate">EXIT:apply</action>
	   </entry>
	 </hbox>
	 <action signal="focus-out-event">EXIT:exit</action>
	</window>'
#	   <vbox homogeneous="true" space-fill="false" space-expand="false">
#	     <button height-request="30" tooltip-text="'$(gettext 'Delete item')'" space-fill="false" space-expand="false">
#	       <input file stock="gtk-delete"></input>
#	       <action>EXIT:delete</action>
#	     </button>
#	   </vbox>
	I=$IFS; IFS=""
	for STATEMENTS in  $(gtkdialog -p Tray_edit -G +"$X"+"$Y"); do
	  eval $STATEMENTS
	done
	IFS=$I
	case $EXIT in
#		delete)
#			grep -v "$TRAY" $WORKDIR/tray_tree > $WORKDIR/tmp
#			mv -f $WORKDIR/tmp $WORKDIR/tray_tree
#			;;
		apply)
			read TMP < $WORKDIR/TRAY_out
			sed -i "s%$TRAY%$TMP%g" $WORKDIR/tray_tree
			;;
	esac
}


export -f load_preset save_preset delete_preset add apply read_settings set_placement_icons set_icons release_event treemenu open_copy edit



  ##########################################################################
  ##                                                                      ##
  ##                             P R O G R A M                            ##
  ##                                                                      ##
  ##########################################################################



#build program-list
ls -1 /usr/share/applications/ | sed 's/.desktop//g' > $WORKDIR/applist
cp -f $WORKDIR/applist $WORKDIR/tray_applist
#build theme-list
ITEMS_THEMES="`find /usr/share/ptheme/globals/ -mindepth 1 -maxdepth 1 -type f -printf "<item>Theme:%f</item>\n"`"

read_settings

REFRESH_ALL='
<action>refresh:ICON1</action>
<action>refresh:ICON2</action>
<action>refresh:ICON3</action>
<action>refresh:ICON4</action>
<action>refresh:VISIBILITY</action>
<action>refresh:HEIGHT</action>
<action>refresh:WIDTH</action>
<action>refresh:DIRECTION_HOR</action>
<action>refresh:DIRECTION_VER</action>
<action>refresh:PAGER_ROWS</action>
<action>refresh:PAGER_COLUMNS</action>
<action>refresh:TASKLIST_STYLE</action>
<action>refresh:TRAY</action>
<action>refresh:BUTTON_TOPLEFT</action>
<action>refresh:BUTTON_LEFT</action>
<action>refresh:BUTTON_BOTTOMLEFT</action>
<action>refresh:BUTTON_TOP</action>
<action>refresh:BUTTON_BOTTOM</action>
<action>refresh:BUTTON_TOPRIGHT</action>
<action>refresh:BUTTON_RIGHT</action>
<action>refresh:BUTTON_BOTTOMRIGHT</action>'

export JWM_tray_load='
<window title="'$(gettext 'Tray presets')'" icon-name="gtk-preferences">
<vbox space-fill="true" space-expand="true">
  <frame '$(gettext 'Manage tray presets ')'>
    <text xalign="0" use-markup="true"><label>"'$(gettext 'Presets contains configuration for one tray or a set of trays. You can save your tray setup for later use. The presets are stored in')'
<i>/usr/share/jwm/tray_templates/</i>
"</label></text> 
    <text xalign="0">
      <variable>TXT_LOAD</variable>
      <input>echo "'$(gettext 'Current tray preset')': $(cat $HOME/.jwm/tray_active_preset)"</input>
    </text>
    <vbox height-request="250" space-fill="true" space-expand="true">
      <tree headers_visible="false">
        <variable>PRESET</variable>
        <input file>'$WORKDIR'/tray_templates</input>
        <action>activate:LOAD_PRESET</action>
      </tree>
    </vbox>
  </frame>
  <hbox space-fill="false" space-expand="false">
    <button space-fill="false" space-expand="false">
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <label>'$(gettext 'Cancel')'</label>
      <action function="closewindow">JWM_tray_load</action>
    </button>
    <text width-request="40" space-fill="true" space-expand="true"><label>""</label></text>
    <button space-fill="false" space-expand="false">
      '"`/usr/lib/gtkdialog/xml_button-icon delete`"'
      <label>'$(gettext 'Delete')'</label>
      <action>delete_preset "$PRESET"</action>
      <action>refresh:PRESET</action>
    </button>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon execute`"'
      <variable>LOAD_PRESET</variable>
      <label>'$(gettext 'Load')'</label>
      <action>load_preset "$PRESET"</action>
      <action>refresh:TXT_LOAD</action>
      '$REFRESH_ALL'
    </button>
  </hbox>
</vbox>
<variable>JWM_tray_load</variable>
</window>'

echo '
<window title="'$(gettext 'Tray-configuration')'" icon-name="gtk-preferences" default-height="450">
<vbox space-fill="true" space-expand="true">
  <vbox space-fill="false" space-expand="false">
    <menubar>
      <menu>
        <menuitem stock="gtk-open" label="'$(gettext 'Open preset')'...">
          <action>launch:JWM_tray_load</action>
          <action>refresh:TXT_LOAD</action>
        </menuitem>
        <menuitem stock="gtk-save" label="'$(gettext 'Save preset')'...">
          <action>save_preset</action>
          <action>refresh:PRESET</action>
        </menuitem>
        <menuitem stock="gtk-delete" label="'$(gettext 'Delete preset')'...">
          <action>launch:JWM_tray_load</action>
          <action>refresh:TXT_LOAD</action>
        </menuitem>
        <menuitemseparator></menuitemseparator>
        <menuitem stock="gtk-quit" label="'$(gettext 'Quit')'">
          <action>EXIT:exit</action>
        </menuitem>
        <label>"'$(gettext 'File')'"</label>
      </menu>
      <menu>
        <menuitem stock="gtk-delete" label="'$(gettext 'Delete tray')'">
          <action>. /usr/lib/gtkdialog/box_yesno "'$(gettext 'Delete tray')'" "'$(gettext 'Do you want to delete this tray?')'"; if [ "$EXIT" = "yes" ]; then echo -n > '$WORKDIR'/tray_tree; apply; fi</action>
          <action>refresh:TRAY</action>
          <action>refresh:ICON1</action>
          <action>refresh:ICON2</action>
          <action>refresh:ICON3</action>
          <action>refresh:ICON4</action>
        </menuitem>
        <menuitemseparator></menuitemseparator>
        <menuitem label="'$(gettext 'Tray color theming')'..."><action>/usr/local/jwm_config/theme_switcher &</action></menuitem>
        <menuitem stock="gtk-edit" label="'$(gettext 'Open copy in text editor')'..."><action>manual_edit</action></menuitem>
        <label>"'$(gettext 'Tray')'"</label>
      </menu>
      <menu>
        <menuitem stock="gtk-help" label="'$(gettext 'Help')'">
          <action>/usr/lib/gtkdialog/box_help "'$(gettext 'JWM tray configuration')'" "http://joewing.net/projects/jwm/config.shtml" &</action>
        </menuitem>
        <label>"'$(gettext 'Help')'"</label>
      </menu>
    </menubar>
    <hbox space-fill="true" space-expand="true">
        <hbox spacing="12" space-fill="false" space-expand="false">
          <button height-request="85" image-position="2" space-fill="false" space-expand="false">
            <variable>ICON1</variable>
            <label>" '$(gettext 'Setup Tray 1')' "</label>
            <input file>'$WORKDIR'/tray_1.svg</input>
            <height>50</height>
            <action>echo 0 > '$WORKDIR'/notebook_page</action>
            <action>refresh:GUI_OPTIONS</action>
            <action>echo true > '$WORKDIR'/tray_radio_null</action>
            <action>refresh:RADIO_NULL</action>
            <action>disable:BOX_TREEBUTTONS</action>
            <action>echo 1 > $WORKDIR/tray_nr</action>
            <action>read_settings</action>
            '$REFRESH_ALL'
          </button>
          <button height-request="85" image-position="2" space-fill="false" space-expand="false">
            <variable>ICON2</variable>
            <label>" '$(gettext 'Setup Tray 2')' "</label>
            <input file>'$WORKDIR'/tray_2.svg</input>
            <height>50</height>
            <action>echo 0 > '$WORKDIR'/notebook_page</action>
            <action>refresh:GUI_OPTIONS</action>
            <action>echo true > '$WORKDIR'/tray_radio_null</action>
            <action>refresh:RADIO_NULL</action>
            <action>disable:BOX_TREEBUTTONS</action>
            <action>echo 2 > $WORKDIR/tray_nr</action>
            <action>read_settings</action>
            '$REFRESH_ALL'
          </button>
          <button height-request="85" image-position="2" space-fill="false" space-expand="false">
            <variable>ICON3</variable>
            <label>" '$(gettext 'Setup Tray 3')' "</label>
            <input file>'$WORKDIR'/tray_3.svg</input>
            <height>50</height>
            <action>echo 0 > '$WORKDIR'/notebook_page</action>
            <action>refresh:GUI_OPTIONS</action>
            <action>echo true > '$WORKDIR'/tray_radio_null</action>
            <action>refresh:RADIO_NULL</action>
            <action>disable:BOX_TREEBUTTONS</action>
            <action>echo 3 > $WORKDIR/tray_nr</action>
            <action>read_settings</action>
            '$REFRESH_ALL'
          </button>
          <button height-request="85" image-position="2" space-fill="false" space-expand="false">
            <variable>ICON4</variable>
            <label>" '$(gettext 'Setup Tray 4')' "</label>
            <input file>'$WORKDIR'/tray_4.svg</input>
            <height>50</height>
            <action>echo 0 > '$WORKDIR'/notebook_page</action>
            <action>refresh:GUI_OPTIONS</action>
            <action>echo true > '$WORKDIR'/tray_radio_null</action>
            <action>refresh:RADIO_NULL</action>
            <action>disable:BOX_TREEBUTTONS</action>
            <action>echo 4 > $WORKDIR/tray_nr</action>
            <action>read_settings</action>
            '$REFRESH_ALL'
          </button>
        </hbox>

      <text width-request="80" space-fill="true" space-expand="true"><label>""</label></text>
      <button image-position="2" space-fill="false" space-expand="false">
        '"`/usr/lib/gtkdialog/xml_button-icon apply.svg big`"'
        <label>"     '$(gettext 'Apply')'     "</label>
        <action>apply</action>
        <action>refresh:ICON1</action>
        <action>refresh:ICON2</action>
        <action>refresh:ICON3</action>
        <action>refresh:ICON4</action>
      </button>
    </hbox>
  </vbox>

  <hseparator></hseparator>

  <hbox space-fill="true" space-expand="true">

    <hbox space-fill="false" space-expand="false">
      <vbox space-fill="true" space-expand="true">
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Placement')'</label>
          <input file>'$WORKDIR'/tray_radio_null</input>
          <variable>RADIO_NULL</variable>
          <action>if true echo 0 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <text height-request="1"><label>""</label></text>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Menus')'</label>
          <input file>'$WORKDIR'/tray_radio_menues</input>
          <variable>RADIO_MENUES</variable>
          <action>if true echo 1 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Buttons')'</label>
          <default>'$DEFAULT_BUTTONS'</default>
          <input file>'$WORKDIR'/tray_radio_buttons</input>
          <variable>RADIO_BUTTONS</variable>
          <action>if true echo 6 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>" '$(gettext 'Desktops')' "</label>
          <input file>'$WORKDIR'/tray_radio_desktops</input>
          <variable>RADIO_DESKTOPS</variable>
          <action>if true echo 4 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Tasklist')'</label>
          <input file>'$WORKDIR'/tray_radio_tasklist</input>
          <variable>RADIO_TASKLIST</variable>
          <action>if true echo 3 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Apps')'</label>
          <input file>'$WORKDIR'/tray_radio_apps</input>
          <variable>RADIO_APPS</variable>
          <action>if true enable:BUTTON_DOCK_ADD</action>
          <action condition="command_is_true([[ `grep -F \"Dock\" '$WORKDIR'/tray_rc` ]] && echo true)">if true disable:BUTTON_DOCK_ADD</action>
          <action>if true echo 2 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Clock')'</label>
          <default>'$DEFAULT_CLOCK'</default>
          <input file>'$WORKDIR'/tray_radio_clock</input>
          <variable>RADIO_CLOCK</variable>
          <action>if true echo 5 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Spacer')'</label>
          <input file>'$WORKDIR'/tray_radio_spacer</input>
          <variable>RADIO_SPACER</variable>
          <action>if true echo 7 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
      </vbox>
    </hbox>

    <hbox space-fill="false" space-expand="false">
      <vbox space-fill="true" space-expand="true">
        <notebook page="'$PAGE_NR'" show-tabs="false" show-border="false" labels="1|2|3|4|5|6|7|8">' > $WORKDIR/tray_xml


               ##################################################
               ##                                              ##
               ##                   T R A Y                    ##
               ##                                              ##
               ##################################################

echo '
<vbox scrollable="true" margin="20" shadow-type="0" hscrollbar-policy="2" vscrollbar-policy="2" space-fill="true" space-expand="true">

  <text use-markup="true" space-fill="false" space-expand="false"><label>"'$(gettext 'Nothing will be stored or activated until you press the <b>Apply</b> button.')'
  "</label></text>
  <hbox>
    <vbox>
      <button relief="2">
        <variable>BUTTON_TOPLEFT</variable>
        '"`/usr/lib/gtkdialog/xml_button-icon $WORKDIR/placement_topleft.svg big`"'
        <action>echo left > '$WORKDIR'/tray_HALIGN; echo top > '$WORKDIR'/tray_VALIGN; add Tray</action>
        <action>refresh:TRAY</action>
        <action>set_placement_icons</action>
        <action>refresh:BUTTON_TOPLEFT</action>
        <action>refresh:BUTTON_LEFT</action>
        <action>refresh:BUTTON_BOTTOMLEFT</action>
        <action>refresh:BUTTON_TOP</action>
        <action>refresh:BUTTON_BOTTOM</action>
        <action>refresh:BUTTON_TOPRIGHT</action>
        <action>refresh:BUTTON_RIGHT</action>
        <action>refresh:BUTTON_BOTTOMRIGHT</action>
      </button>
      <button relief="2">
        <variable>BUTTON_LEFT</variable>
        '"`/usr/lib/gtkdialog/xml_button-icon $WORKDIR/placement_left.svg big`"'
        <action>echo left > '$WORKDIR'/tray_HALIGN; echo center > '$WORKDIR'/tray_VALIGN; add Tray</action>
        <action>refresh:TRAY</action>
        <action>set_placement_icons</action>
        <action>refresh:BUTTON_TOPLEFT</action>
        <action>refresh:BUTTON_LEFT</action>
        <action>refresh:BUTTON_BOTTOMLEFT</action>
        <action>refresh:BUTTON_TOP</action>
        <action>refresh:BUTTON_BOTTOM</action>
        <action>refresh:BUTTON_TOPRIGHT</action>
        <action>refresh:BUTTON_RIGHT</action>
        <action>refresh:BUTTON_BOTTOMRIGHT</action>
      </button>
      <button relief="2">
        <variable>BUTTON_BOTTOMLEFT</variable>
        '"`/usr/lib/gtkdialog/xml_button-icon $WORKDIR/placement_bottomleft.svg big`"'
        <action>echo left > '$WORKDIR'/tray_HALIGN; echo bottom > '$WORKDIR'/tray_VALIGN; add Tray</action>
        <action>refresh:TRAY</action>
        <action>set_placement_icons</action>
        <action>refresh:BUTTON_TOPLEFT</action>
        <action>refresh:BUTTON_LEFT</action>
        <action>refresh:BUTTON_BOTTOMLEFT</action>
        <action>refresh:BUTTON_TOP</action>
        <action>refresh:BUTTON_BOTTOM</action>
        <action>refresh:BUTTON_TOPRIGHT</action>
        <action>refresh:BUTTON_RIGHT</action>
        <action>refresh:BUTTON_BOTTOMRIGHT</action>
      </button>
    </vbox>
    <vbox>
      <button relief="2">
        <variable>BUTTON_TOP</variable>
        '"`/usr/lib/gtkdialog/xml_button-icon $WORKDIR/placement_top.svg big`"'
        <action>echo center > '$WORKDIR'/tray_HALIGN; echo top > '$WORKDIR'/tray_VALIGN; add Tray</action>
        <action>refresh:TRAY</action>
        <action>set_placement_icons</action>
        <action>refresh:BUTTON_TOPLEFT</action>
        <action>refresh:BUTTON_LEFT</action>
        <action>refresh:BUTTON_BOTTOMLEFT</action>
        <action>refresh:BUTTON_TOP</action>
        <action>refresh:BUTTON_BOTTOM</action>
        <action>refresh:BUTTON_TOPRIGHT</action>
        <action>refresh:BUTTON_RIGHT</action>
        <action>refresh:BUTTON_BOTTOMRIGHT</action>
      </button>
      <button relief="2" tooltip-text="'$(gettext 'Delete tray')'">
        '"`/usr/lib/gtkdialog/xml_button-icon $WORKDIR/placement_none.svg big`"'
        <action>. /usr/lib/gtkdialog/box_yesno "'$(gettext 'Delete tray')'" "'$(gettext 'Do you want to delete this tray?')'"; if [ "$EXIT" = "yes" ]; then echo -n > '$WORKDIR'/tray_tree; add Tray; apply; fi</action>
        <action>refresh:TRAY</action>
        <action>refresh:ICON1</action>
        <action>refresh:ICON2</action>
        <action>refresh:ICON3</action>
        <action>refresh:ICON4</action>
        <action>set_placement_icons</action>
        <action>refresh:BUTTON_TOPLEFT</action>
        <action>refresh:BUTTON_LEFT</action>
        <action>refresh:BUTTON_BOTTOMLEFT</action>
        <action>refresh:BUTTON_TOP</action>
        <action>refresh:BUTTON_BOTTOM</action>
        <action>refresh:BUTTON_TOPRIGHT</action>
        <action>refresh:BUTTON_RIGHT</action>
        <action>refresh:BUTTON_BOTTOMRIGHT</action>
      </button>
      <button relief="2">
        <variable>BUTTON_BOTTOM</variable>
        '"`/usr/lib/gtkdialog/xml_button-icon $WORKDIR/placement_bottom.svg big`"'
        <action>echo center > '$WORKDIR'/tray_HALIGN; echo bottom > '$WORKDIR'/tray_VALIGN; add Tray</action>
        <action>refresh:TRAY</action>
        <action>set_placement_icons</action>
        <action>refresh:BUTTON_TOPLEFT</action>
        <action>refresh:BUTTON_LEFT</action>
        <action>refresh:BUTTON_BOTTOMLEFT</action>
        <action>refresh:BUTTON_TOP</action>
        <action>refresh:BUTTON_BOTTOM</action>
        <action>refresh:BUTTON_TOPRIGHT</action>
        <action>refresh:BUTTON_RIGHT</action>
        <action>refresh:BUTTON_BOTTOMRIGHT</action>
      </button>
    </vbox>
    <vbox>
      <button relief="2">
        <variable>BUTTON_TOPRIGHT</variable>
        '"`/usr/lib/gtkdialog/xml_button-icon $WORKDIR/placement_topright.svg big`"'
        <action>echo right > '$WORKDIR'/tray_HALIGN; echo top > '$WORKDIR'/tray_VALIGN; add Tray</action>
        <action>refresh:TRAY</action>
        <action>set_placement_icons</action>
        <action>refresh:BUTTON_TOPLEFT</action>
        <action>refresh:BUTTON_LEFT</action>
        <action>refresh:BUTTON_BOTTOMLEFT</action>
        <action>refresh:BUTTON_TOP</action>
        <action>refresh:BUTTON_BOTTOM</action>
        <action>refresh:BUTTON_TOPRIGHT</action>
        <action>refresh:BUTTON_RIGHT</action>
        <action>refresh:BUTTON_BOTTOMRIGHT</action>
      </button>
      <button relief="2">
        <variable>BUTTON_RIGHT</variable>
        '"`/usr/lib/gtkdialog/xml_button-icon $WORKDIR/placement_right.svg big`"'
        <action>echo right > '$WORKDIR'/tray_HALIGN; echo center > '$WORKDIR'/tray_VALIGN; add Tray</action>
        <action>refresh:TRAY</action>
        <action>set_placement_icons</action>
        <action>refresh:BUTTON_TOPLEFT</action>
        <action>refresh:BUTTON_LEFT</action>
        <action>refresh:BUTTON_BOTTOMLEFT</action>
        <action>refresh:BUTTON_TOP</action>
        <action>refresh:BUTTON_BOTTOM</action>
        <action>refresh:BUTTON_TOPRIGHT</action>
        <action>refresh:BUTTON_RIGHT</action>
        <action>refresh:BUTTON_BOTTOMRIGHT</action>
      </button>
      <button relief="2">
        <variable>BUTTON_BOTTOMRIGHT</variable>
        '"`/usr/lib/gtkdialog/xml_button-icon $WORKDIR/placement_bottomright.svg big`"'
        <action>echo right > '$WORKDIR'/tray_HALIGN; echo bottom > '$WORKDIR'/tray_VALIGN; add Tray</action>
        <action>refresh:TRAY</action>
        <action>set_placement_icons</action>
        <action>refresh:BUTTON_TOPLEFT</action>
        <action>refresh:BUTTON_LEFT</action>
        <action>refresh:BUTTON_BOTTOMLEFT</action>
        <action>refresh:BUTTON_TOP</action>
        <action>refresh:BUTTON_BOTTOM</action>
        <action>refresh:BUTTON_TOPRIGHT</action>
        <action>refresh:BUTTON_RIGHT</action>
        <action>refresh:BUTTON_BOTTOMRIGHT</action>
      </button>
    </vbox>
 
    <vseparator width-request="40"></vseparator>

    <vbox>
      <vbox tooltip-text="'$(gettext 'Direction of the tray')'">
        <radiobutton>
          <variable>DIRECTION_HOR</variable>
          <label>'$(gettext 'Horizontal Tray')'</label>
          <input file>'$WORKDIR'/tray_DIRECTION_HOR</input>
          <action signal="realize">if true hide:BOX_TRAY_WIDTH</action>
          <action>if true show:BOX_TRAY_HEIGHT</action>
          <action>if true hide:BOX_TRAY_WIDTH</action>
          <action>if true add Tray</action>
          <action>if true refresh:TRAY</action>
        </radiobutton>
        <radiobutton>
          <variable>DIRECTION_VER</variable>
          <input file>'$WORKDIR'/tray_DIRECTION_VER</input>
          <label>'$(gettext 'Vertical Tray')'</label>
          <action signal="realize">if true hide:BOX_TRAY_HEIGHT</action>
          <action>if true hide:BOX_TRAY_HEIGHT</action>
          <action>if true show:BOX_TRAY_WIDTH</action>
          <action>if true add Tray</action>
          <action>if true refresh:TRAY</action>
        </radiobutton>
      </vbox>
      <hbox space-fill="false" space-expand="false">
        <text xalign="0"><label>'$(gettext 'Height (px)')'</label></text>
        <entry editable="true" allow-empty="false" width-request="40">
          <input file>'$WORKDIR'/tray_HEIGHT</input>
          <variable>HEIGHT</variable>
          <action signal="focus-out-event">if [ "`echo "$HEIGHT" | grep -v "[a-z]" | grep "[0-9]"`" ]; then echo $HEIGHT > '$WORKDIR'/tray_HEIGHT; fi</action>
          <action signal="focus-out-event">refresh:HEIGHT</action>
          <action signal="focus-out-event">add Tray</action>
          <action signal="focus-out-event">refresh:TRAY</action>
        </entry>
        <variable>BOX_TRAY_HEIGHT</variable>
      </hbox>
      <hbox space-fill="false" space-expand="false">
        <text xalign="0"><label>'$(gettext 'Width (px)')'</label></text>
        <entry editable="true" allow-empty="false" width-request="40">
          <input file>'$WORKDIR'/tray_WIDTH</input>
          <variable>WIDTH</variable>
          <action signal="focus-out-event">if [ "`echo "$WIDTH" | grep -v "[a-z]" | grep "[0-9]"`" ]; then echo $WIDTH > '$WORKDIR'/tray_WIDTH; fi</action>
          <action signal="focus-out-event">refresh:WIDTH</action>
          <action signal="focus-out-event">add Tray</action>
          <action signal="focus-out-event">refresh:TRAY</action>
        </entry>
        <variable>BOX_TRAY_WIDTH</variable>
      </hbox>
      <text xalign="0"><label>"
'$(gettext 'Visibility')'"</label></text>
      <comboboxtext width-request="150">
        <variable>VISIBILITY</variable>
        <input file>'$WORKDIR'/tray_VISIBILITY</input>
        <item>'$(gettext 'Always visible')'</item>
        <item>'$(gettext 'Hidden when not in use')'</item>
        <item>'$(gettext 'Visible behind running apps')'</item>
        <item>'$(gettext 'Hybrid: Visible behind, hidden above')'</item>
        <action>add Tray</action>
        <action>refresh:TRAY</action>
      </comboboxtext>
    </vbox>
  </hbox>
</vbox>
' >> $WORKDIR/tray_xml


               ##################################################
               ##                                              ##
               ##             M E N U    B U T T O N           ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <vbox margin="20">
    <text><label>"'$(gettext 'Add menu-button. Both icon and label are optional, but you have to include at least one of them.')'
    "</label></text>
    <hbox homogeneous="true" space-fill="true" space-expand="true">
      <text xalign="1"><label>"'$(gettext 'Menu type')'  "</label></text>
      <comboboxtext>
        <variable>MENU</variable>
        <item>'$(gettext 'Applications')'</item>
        <item>'$(gettext 'Bookmarks')'</item>
        <item>'$(gettext 'Places')'</item>
        <action>echo "$MENU" > '$WORKDIR'/tray_MENU_LABEL</action>
        <action>if [ "$MENU" = "'$(gettext 'Applications')'" ]; then echo "/usr/share/pixmaps/puppy/puppy.svg" > '$WORKDIR'/tray_MENU_ICON; fi</action>
        <action>if [ "$MENU" = "'$(gettext 'Bookmarks')'" ]; then echo "/usr/share/pixmaps/puppy/bookmark.svg" > '$WORKDIR'/tray_MENU_ICON; fi</action>
        <action>if [ "$MENU" = "'$(gettext 'Places')'" ]; then echo "/usr/share/pixmaps/puppy/directory.svg" > '$WORKDIR'/tray_MENU_ICON; fi</action>
        <action>refresh:MENU_LABEL</action>
        <action>refresh:MENU_ICON</action>
      </comboboxtext>
    </hbox>
    <hbox homogeneous="true" space-fill="true" space-expand="true">
      <text xalign="1"><label>"'$(gettext 'Label')'  "</label></text>
      <entry space-fill="true" space-expand="true">
        <variable>MENU_LABEL</variable>
        <input file>'$WORKDIR'/tray_MENU_LABEL</input>
        <default>"'$(gettext 'Menu')'"</default>
      </entry>
    </hbox>
    <hbox>
      <checkbox space-fill="false" space-expand="false">
        <variable>MENU_ICON_CHK</variable>
        <label>" '$(gettext 'Include icon')' "</label>
        <action>if true enable:HBOX_ICON</action>
        <action>if false disable:HBOX_ICON</action>
      </checkbox>
      <hbox space-fill="true" space-expand="true">
        <entry fs-action="file" fs-folder="/usr/share/pixmaps/puppy/" space-fill="true" space-expand="true">
          <variable>MENU_ICON</variable>
          <input file>'$WORKDIR'/tray_MENU_ICON</input>
          <default>"/usr/share/pixmaps/puppy/puppy.svg"</default>
        </entry>
        <button space-fill="false" space-expand="false">
          <variable>MENU_ICON_BTN</variable>
          '"`/usr/lib/gtkdialog/xml_button-icon $WORKDIR/tray_menuicon.svg`"'
          <action>fileselect:MENU_ICON</action>
          <action>rm $WORKDIR/tray_menuicon.svg 2>/dev/null; ln -s $MENU_ICON $WORKDIR/tray_menuicon.svg</action>
          <action>refresh:MENU_ICON_BTN</action>
        </button>
        <variable>HBOX_ICON</variable>
        <sensitive>false</sensitive>
      </hbox>
    </hbox>
    <text><label>""</label></text>
    <hbox>
      <button>
        <label>" '$(gettext 'Add menu')' "</label>
        '"`/usr/lib/gtkdialog/xml_button-icon add`"'
        <action>add Menu $MENU "$MENU_LABEL" "$MENU_ICON"</action>
        <action>refresh:TRAY</action>
      </button>
    </hbox>
  </vbox>
</vbox>' >> $WORKDIR/tray_xml
   

               ##################################################
               ##                                              ##
               ##                   A P P S                    ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <vbox margin="20">
    <text><label>"'$(gettext 'Generally, running tray-apps place themself in the Dock area. Only one Dock area is supported.')'"</label></text>
    <hbox>
      <button>
        <variable>BUTTON_DOCK_ADD</variable>
        <label>" '$(gettext 'Add Dock area')' "</label>
        '"`/usr/lib/gtkdialog/xml_button-icon add`"'
        <action>add "Dock area"</action>
        <action>refresh:TRAY</action>
      </button>
    </hbox>

    <hseparator height-request="40"></hseparator>

    <text><label>"'$(gettext 'In addition to docked apps, you can define specific apps to appear (swallowed) in the tray. Choose from the list or edit to swallow app (few apps allow swallowing).')'"</label></text>
    <hbox>
      <text><label>""</label></text>
      <vbox>
        <hbox>
          <text xalign="0" space-fill="true" space-expand="true"><label>'$(gettext 'Command')'</label></text>
          <comboboxentry>
            <variable>SWALLOW</variable>' >> $WORKDIR/tray_xml
            [ "`which asapm`" ]			&& echo '<item>asapmshell -u 4 -status PaleGreen3 -green PaleGreen3</item>' >> $WORKDIR/tray_xml
            [ "`which xload`" ]			&& echo '<item>xload -nolabel -fg "#225F7C" -hl white -bg "#222"</item>' >> $WORKDIR/tray_xml
            [ "`which freememapplet`" ]	&& echo '<item>freememappletshell</item>' >> $WORKDIR/tray_xml
            [ "`which blinkydelayed`" ]	&& echo '<item>blinkydelayed -bg "#939191"</item>' >> $WORKDIR/tray_xml
            [ "`which xvkbd`" ]			&& echo '<item>xvkbd -geometry 800x300 -no-keypad</item>' >> $WORKDIR/tray_xml
          echo '</comboboxentry>

        </hbox>
        <hbox>
          <text xalign="0" space-fill="true" space-expand="true"><label>'$(gettext 'Height (optional)')'</label></text>
          <entry editable="true" allow-empty="false" width-request="40" space-fill="false" space-expand="false">
            <input file>'$WORKDIR'/tray_SWALLOW_HEIGHT</input>
            <variable>SWALLOW_HEIGHT</variable>
            <action signal="focus-out-event">if [ "`echo "$SWALLOW_HEIGHT" | grep -v "[a-z]" | grep "[0-9]"`" ]; then echo $SWALLOW_HEIGHT > '$WORKDIR'/tray_SWALLOW_HEIGHT; fi</action>
            <action signal="focus-out-event">refresh:SWALLOW_HEIGHT</action>
          </entry>
        </hbox>
        <hbox>
          <text xalign="0" space-fill="true" space-expand="true"><label>'$(gettext 'Width (optional)')'</label></text>
          <entry editable="true" allow-empty="false" width-request="40" space-fill="false" space-expand="false">
            <input file>'$WORKDIR'/tray_SWALLOW_WIDTH</input>
            <variable>SWALLOW_WIDTH</variable>
            <action signal="focus-out-event">if [ "`echo "$SWALLOW_WIDTH" | grep -v "[a-z]" | grep "[0-9]"`" ]; then echo $SWALLOW_WIDTH > '$WORKDIR'/tray_SWALLOW_WIDTH; fi</action>
            <action signal="focus-out-event">refresh:SWALLOW_WIDTH</action>
          </entry>
        </hbox>
      </vbox>
      <text><label>""</label></text>
      <button image-position="2">
        <label>" '$(gettext 'Swallow app')' "</label>
        '"`/usr/lib/gtkdialog/xml_button-icon add`"'
        <action>add Swallowed</action>
        <action>refresh:TRAY</action>
      </button>
    </hbox>
  </vbox>
</vbox>' >> $WORKDIR/tray_xml




               ##################################################
               ##                                              ##
               ##               T A S K L I S T                ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <vbox margin="20">
    <text><label>"'$(gettext 'Show running programs in tray.
NOTE! The tasklist makes the tray stretch horizontaly.')'
    "</label></text>
    <hbox>
      <text xalign="0" space-fill="true" space-expand="true"><label>"'$(gettext 'How programs show up in tray')'"</label></text>
      <comboboxtext space-fill="false" space-expand="false">
        <variable>TASKLIST_STYLE</variable>
        <input file>'$WORKDIR'/tray_TASKLIST_STYLE</input>
        <item>'$(gettext 'Iconify')'</item>
        <item>'$(gettext 'Fixed width')'</item>
        <item>'$(gettext 'Variable width')'</item>
      </comboboxtext>
    </hbox>
    <text><label>""</label></text>
    <hbox>
      <button>
        <label>" '$(gettext 'Set Tasklist')' "</label>
        '"`/usr/lib/gtkdialog/xml_button-icon add`"'
        <action>add Tasklist</action>
        <action>refresh:TRAY</action>
      </button>
    </hbox>
  </vbox>
</vbox>' >> $WORKDIR/tray_xml


               ##################################################
               ##                                              ##
               ##                D E S K T O P S               ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <vbox margin="20">
    <text><label>"'$(gettext "Define how many virtual desktops to show.")'
    "</label></text>
    <hbox>
      <vbox>
        <hbox>
          <text><label>"'$(gettext 'Rows')'"</label></text>
          <comboboxtext width-request="50">
            <variable>PAGER_ROWS</variable>
            <input file>'$WORKDIR'/tray_PAGER_ROWS</input>
            <item>1</item><item>2</item>
            <action>add Pager</action>
            <action>refresh:TRAY</action>
          </comboboxtext>
        </hbox>
        <hbox>
          <text><label>"'$(gettext 'Columns')'"</label></text>
          <comboboxtext width-request="50">
            <variable>PAGER_COLUMNS</variable>
            <input file>'$WORKDIR'/tray_PAGER_COLUMNS</input>
            <item>1</item><item>2</item><item>3</item><item>4</item><item>5</item>
            <action>add Pager</action>
            <action>refresh:TRAY</action>
          </comboboxtext>
        </hbox>
      </vbox>
      <button image-position="2">
        <label>"'$(gettext 'Add / update')'"</label>
        '"`/usr/lib/gtkdialog/xml_button-icon add`"'
        <action>[ ! "`grep -F "Pager" '$WORKDIR'/tray_rc`" ] && add "Pager"</action>
        <action>refresh:TRAY</action>
      </button>
    </hbox>
  </vbox>
</vbox>' >> $WORKDIR/tray_xml


               ##################################################
               ##                                              ##
               ##                   C L O C K                  ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <vbox margin="20">
    <text><label>"'$(gettext 'The clock font/color is set by the graphical theme.')'
    "</label></text>
    <hbox>
      <text><label>'$(gettext 'Clock format')'</label></text>
      <comboboxtext width-request="200">
        <variable>CLOCK</variable>
        <item>""</item>
        <item>12:mm</item>
        <item>12:mm AM</item>
        <item>12:mm:ss</item>
        <item>12:mm:ss AM</item>
        <item>24:mm</item>
        <item>24:mm:ss</item>
        <item>'$(gettext 'day')' '$(gettext 'date')'. '$(gettext 'month')' 24:mm</item>
        <item>'$(gettext 'date')'/'$(gettext 'month')' 24:mm</item>
        <item>'$(gettext 'full date and time')'</item>
        <action>add Clock</action>
        <action>refresh:TRAY</action>
      </comboboxtext>
    </hbox>
    <hbox>
      <text><label>'$(gettext 'Press-action')'</label></text>
      <comboboxentry width-request="200">
        <variable>CLOCK_ACTION</variable>
        <item>'$(gettext 'Clock menu')'</item>
        <item>'$(gettext 'Clock tools')'</item>' >> $WORKDIR/tray_xml
        [ "`which minixcal`" ]				&& echo '<item>minixcal</item>' >> $WORKDIR/tray_xml
        [ "`which pclock`" ]				&& echo '<item>pclock</item>' >> $WORKDIR/tray_xml
        [ "`which set-time-for-puppy`" ]	&& echo '<item>set-time-for-puppy</item>' >> $WORKDIR/tray_xml
        echo '
        <item>'$(gettext 'Show desktop')'</item>
        <action>add Clock</action>
        <action>refresh:TRAY</action>
      </comboboxentry>
    </hbox>
    <text><label>""</label></text>
    <hbox>
      <button>
        '"`/usr/lib/gtkdialog/xml_button-icon add`"'
        <label>" '$(gettext 'Add clock')' "</label>
        <action>add Clock</action>
        <action>refresh:TRAY</action>
      </button>
    </hbox>
  </vbox>
</vbox>' >> $WORKDIR/tray_xml

   
               ##################################################
               ##                                              ##
               ##                 B U T T O N                  ##
               ##                                              ##
               ##################################################

echo '
<hbox space-expand="true" space-fill="true">
  <text width-request="10" space-expand="false" space-fill="false"><label>""</label></text>
  <vbox margin="20" space-expand="true" space-fill="true">
    <text space-expand="false" space-fill="false"><label>"'$(gettext 'Click to add button')'"</label></text>
    <hbox>
      <text angle="90" yalign="0" wrap="false" use-markup="true" space-expand="false" space-fill="false"><label>"<b>'$(gettext 'Functions')'</b>"</label></text>
      <tree hover-selection="true" headers_visible="false">
        <variable>FUNCTION</variable>
        <item>'$(gettext 'Show desktop')'</item>' >> $WORKDIR/tray_xml
        [ "`which xlock`" ] && echo "<item>$(gettext 'Lock screen')</item>" >> $WORKDIR/tray_xml
        [ -s "/usr/share/doc/index.html" ] && echo "<item>$(gettext 'Puppy Help')</item>" >> $WORKDIR/tray_xml
        echo '<item>'$(gettext 'Leave')'</item>
        <item>'$(gettext 'Shutdown NOW')'</item>
        <item>'$(gettext 'Reboot NOW')'</item>
        <item>'$(gettext 'Edit tray')'</item>
        '$ITEMS_THEMES'
        <action signal="button-release-event">add "Function button"</action>
        <action signal="button-release-event">refresh:TRAY</action>
      </tree>
    </hbox>
    <text><label>""</label></text>
    <hbox space-expand="true" space-fill="true">
      <text angle="90" yalign="0" wrap="false" use-markup="true" space-expand="false" space-fill="false"><label>"<b>'$(gettext 'Applications')'</b>"</label></text>
      <vbox>
        <hbox>
          <entry secondary-icon-stock="gtk-find">
            <variable>PROGRAM_SEARCH</variable>
            <action signal="key-release-event">grep -F "$PROGRAM_SEARCH" '$WORKDIR'/applist > '$WORKDIR'/tray_applist</action>
            <action signal="key-release-event">refresh:PROGRAM</action>
          </entry>
        </hbox>
        <tree hover-selection="true" headers_visible="false" space-expand="true" space-fill="true">
          <variable>PROGRAM</variable>
          <input file>'$WORKDIR'/tray_applist</input>
          <action signal="button-release-event">add "Program button"</action>
          <action signal="button-release-event">refresh:TRAY</action>
          ### add pure <action> for keyboard navigation
          <action>add "Program button"</action>
          <action>refresh:TRAY</action>
        </tree>
      </vbox>
    </hbox>
  </vbox>
  <text width-request="20" space-expand="false" space-fill="false"><label>""</label></text>
</hbox>' >> $WORKDIR/tray_xml


               ##################################################
               ##                                              ##
               ##                  S P A C E R                 ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <vbox margin="20">
    <text><label>"'$(gettext 'Set the height/width to 0, and the spacer will fill.')'
    "</label></text>
    <hbox>
      <text><label>"'$(gettext 'Width')' "</label></text>
      <spinbutton range-max="999" width-request="47">
        <variable>SPACER</variable>
        <default>10</default>
      </spinbutton>
    </hbox>
    <text><label>""</label></text>
    <hbox>
      <button>
        '"`/usr/lib/gtkdialog/xml_button-icon add`"'
        <label>" '$(gettext 'Add empty gap')' "</label>
        <action>add Spacer</action>
        <action>refresh:TRAY</action>
      </button>
    </hbox>
  </vbox>
</vbox>' >> $WORKDIR/tray_xml




          echo '<variable>GUI_OPTIONS</variable>
          <input file>'$WORKDIR'/notebook_page</input>
        </notebook>
      </vbox>
    </hbox>

    <vseparator space-fill="false" space-expand="false"></vseparator>
    <text width-request="0" space-fill="false" space-expand="false"><label>""</label></text>
    <vbox space-fill="true" space-expand="true">
      <tree name="rcTree" headers_visible="true" hover-selection="true" exported_column="1" reorderable="true" column-header-active="false|false" space-fill="true" space-expand="true">
        <label>"'$(gettext 'Tray setup (left/top to right/bottom)')'|'$(gettext 'jwmrc syntax')'"</label>
        <variable>TRAY</variable>
        <input file icon-column="0">'$WORKDIR'/tray_tree</input>
        <output file>'$WORKDIR'/tray_tree_out</output>
        <action signal="enter-notify-event">echo " '$(gettext "Mouse middle-click to remove item from list     ---     Drag'n drop to move item up/down")'" > '$WORKDIR'/tray_statusbar</action>
        <action signal="leave-notify-event">echo -n " '$(gettext 'Current tray preset')': " > '$WORKDIR'/tray_statusbar; cat $HOME/.jwm/tray_active_preset >> '$WORKDIR'/tray_statusbar</action>
        <action signal="button-press-event">grep -F "$TRAY" '$WORKDIR'/tray_tree > '$WORKDIR'/tray_press_event; [ "$TRAY" ] && echo #menu_tray</action>
        <action signal="button-release-event" condition="command_is_true( echo $PTR_Y )">release_event</action>
        <action signal="button-release-event" condition="command_is_true( echo $PTR_Y )">refresh:TRAY</action>
        <action signal="button-release-event" condition="command_is_true( echo $PTR_Y )">break:</action>
        ### code below is activated when user moves track in queue. --> $PTR_Y gets 0
        <action signal="button-release-event">activate:TRAY_SAVE</action> 
      </tree>
      ### use an extern <button> to make user-defined <action> run _after_ builtin <action> (the opposite happens when a signal is defined).
      <button visible="false"> 
        <variable>TRAY_SAVE</variable>
        <action>cp '$WORKDIR'/tray_tree '$WORKDIR'/tray_tree_backup</action> 
        <action>save:TRAY</action>
        <action>cut -d"|" -f 1,3,4 '$WORKDIR'/tray_tree_out > '$WORKDIR'/tray_tree</action>
        ###run a check if moved item was dropped onto another item --> will delete the moved item --> we will then use last backup
        <action condition="command_is_true([[ $(grep -c / '$WORKDIR'/tray_tree_out) != $(grep -c / '$WORKDIR'/tray_tree_backup) ]] && cp '$WORKDIR'/tray_tree_backup '$WORKDIR'/tray_tree && echo true )">refresh:TRAY</action> 
      </button>
    </vbox>
  </hbox>

  <vbox space-expand="false" space-fill="false">
    <hbox space-expand="true" space-fill="true">
      <statusbar file-monitor="true" auto-refresh="true" height-request="23">
        <variable>STATUSBAR</variable>
        <input file>'$WORKDIR'/tray_statusbar</input>
      </statusbar>
    </hbox>
  </vbox>
  
</vbox>
<action signal="show">kill '$XPID'</action>
<action signal="delete-event" function="closewindow">JWM_tray_load</action>
</window>' >> $WORKDIR/tray_xml

sed -i 's/###.*//' $WORKDIR/tray_xml #triple hash (###) for comments. --> as #FF0000 is valid xml-code --> and because double hash is in use by bash: ${VAR##*/} 
gtkdialog -f $WORKDIR/tray_xml
